package com.hdaccp.xingmu.controller;

import com.hdaccp.xingmu.dao.CostitemDao;
import com.hdaccp.xingmu.dao.LinDao;
import com.hdaccp.xingmu.dao.OrderDao;
import com.hdaccp.xingmu.dao.house;
import com.hdaccp.xingmu.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


@Controller
@RequestMapping("/lin")
public class LinCon {
    @Autowired
    private LinDao linDao;
    @Autowired
    private CostitemDao costitemDao;
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private house houseDao;

    @Autowired
    private EntityManager entityManager;


    @RequestMapping("/a")
    @ResponseBody
    public void a(long id){

        List<Costitem> list = costitemDao.findAll();
        System.out.println(list.size());
        House house = houseDao.findById(id).get();
        System.out.println("house"+"----"+house.getId());
        for(Costitem c:list){

            Lin lin = new Lin();

            lin.setLasttime(0);

            java.util.Date utilDate = new java.util.Date();
            java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());


            lin.setSdate(sqlDate);
            lin.setBdate(sqlDate);
            lin.setJdate(sqlDate);


            lin.setOrderhao(null);

            lin.setHouse(house);


            lin.setCostitem(c);

            lin.setNum(10);
            lin.setZmoney(500);
            lin.setType(1);

            Lin l = linDao.save(lin);
        }

    }


    @RequestMapping("/list")
    @ResponseBody
    public List<Lin> lin(String name){

        String sql ="from Lin where costitem.id = "+name;

        Query query = entityManager.createQuery(sql);

        List<Lin> uu = query.getResultList();



        return uu;
    }

    @RequestMapping("/add")
    public void add(Lin lin){

    }


}
